<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/10/25
 * Time: 18:04
 * 店铺优惠券页面
 */

namespace Home\Controller;

use Think\Controller;
use think\Session;

class CouponController extends Controller
{
    public $customer_id = '';
    public $store_id = 0;

    /*
     * 初始化
     */
    public function _initialize()
    {
        if (session('?customer_id') && session('?STORE_ID')) {
            $this->customer_id = session('customer_id');//用户id
            $this->store_id = session('STORE_ID');//店铺id
        }
        $this->customer_id = "20181102115031877722";
        $this->store_id = '11';
    }

    /**
     * 店铺优惠券领取列表页面
     */
    public function index()
    {
        if ($this->store_id) {
            $list = M('coupon')->field('coupon_id,money,condition_money,use_start_time,use_end_time')->where(['store_id' => $this->store_id, 'status' => 1, 'type' => 1])->select();
            //判断当前店铺的优惠券是否已经领取过
            foreach($list as $k=>$v){
                $check_coupon = M('user_coupon')->where(['store_id' => $this->store_id, 'customer_id' => $this->customer_id, 'coupon_id' => $v['coupon_id']])->find();
                if($check_coupon){
                    $list[$k]['status']='已领取';
                }else{
                    $list[$k]['status']='立即领取';
                }
            }

//            echo "<pre>";
//            print_r($list);die;
        } else {
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }
        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 领取优惠券
     */
    public function ajax_getCoupon()
    {
        $coupon_id = I('get.coupon_id');
        if ($coupon_id) {
            $data['get_time'] = time();
            $data['coupon_id'] = $coupon_id;
            $data['customer_id'] = $this->customer_id;
            $data['store_id'] = $this->store_id;

            //判断是否重复领取
            $check_coupon = M('user_coupon')->where(['store_id' => $this->store_id, 'customer_id' => $this->customer_id, 'coupon_id' => $coupon_id])->find();
            if ($check_coupon) {
                $this->ajaxReturn(['state' => -2, 'msg' => '已经领取过了']);
            } else {
                $row = M('user_coupon')->add($data);
                if ($row > 0) {
                    $this->ajaxReturn(['state' => 1, 'msg' => '领取成功']);
                } else {
                    $this->ajaxReturn(['state' => 0, 'msg' => '请求失败请重试']);
                }
            }
        } else {
            $this->ajaxReturn(['state' => -1, 'msg' => '缺少参数请检查']);
        }
    }

    /**
     * 优惠券列表
     */
    public function myList(){
        $this->display();
    }

    /**
     * 获取优惠券的接口
     */
    public function getCouponList(){
        //优惠券类型 0未使用1已使用2已过期'
        if ($this->store_id && $this->customer_id) {

            //获取优惠券的状态
            if(I('get.status')){
                $status = I('get.status');
            }else{
                $status = 0;
            }

            //查询用户的拥有的优惠券信息
            //判断优惠券的是否过期
            $user_coupon = M('user_coupon')->alias('uc')
                ->field('c.coupon_id')
                ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                ->where(array('c.use_end_time' => ['elt', time()], 'uc.customer_id' => $this->customer_id))
                ->select();
            if ($user_coupon) {
                foreach ($user_coupon as $value) {
                    //修改过期优惠券状态
                    M('user_coupon')->where(['coupon_id' => $value['coupon_id']])->setField(['status' => 2]);
                }
            }
            $where['uc.customer_id'] = $this->customer_id;
            $return = [];

            switch ($status) {
                case 0://未使用
                    $where['uc.status'] = 0;
                    $result = M('user_coupon')->alias('uc')
                        ->field("uc.customer_id,uc.coupon_id,uc.store_id,uc.status,c.money,c.condition_money,c.use_start_time,c.use_end_time,FROM_UNIXTIME(c.use_end_time,'%Y-%m-%d') as endTime,FROM_UNIXTIME(c.use_start_time,'%Y-%m-%d') as startTime")
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();
                    break;
                case 1://已使用
                    $where['uc.status'] = 1;
                    $result = M('user_coupon')->alias('uc')
                       ->field("uc.customer_id,uc.coupon_id,uc.store_id,uc.status,c.money,c.condition_money,c.use_start_time,c.use_end_time,FROM_UNIXTIME(c.use_end_time,'%Y-%m-%d') as endTime,FROM_UNIXTIME(c.use_start_time,'%Y-%m-%d') as startTime")
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();
                    break;
                case 2://已过期
                    $where['uc.status'] = 2;
                    $result = M('user_coupon')->alias('uc')
                        ->field("uc.customer_id,uc.coupon_id,uc.store_id,uc.status,c.money,c.condition_money,c.use_start_time,c.use_end_time,FROM_UNIXTIME(c.use_end_time,'%Y-%m-%d') as endTime,FROM_UNIXTIME(c.use_start_time,'%Y-%m-%d') as startTime")
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();
                    break;
                default:
                    $where['uc.status'] = 0;
                    $result = M('user_coupon')->alias('uc')
                        ->field("uc.customer_id,uc.coupon_id,uc.store_id,,uc.status,c.money,c.condition_money,c.use_start_time,c.use_end_time,FROM_UNIXTIME(c.use_end_time,'%Y-%m-%d')as endTime,FROM_UNIXTIME(c.use_start_time,'%Y-%m-%d') as startTime")
                        ->join('meal_coupon as c on uc.coupon_id=c.coupon_id', 'left')
                        ->where($where)
                        ->select();
            }

            if (!empty($result)) {
                $this->ajaxreturn(array('state' => 2, 'data' => $result,'msg'=>'获取优惠券成功！'), 'json');
            } else {
                $this->ajaxreturn(array('state' => 0, 'data' => [],'msg'=>'暂无数据！'), 'json');

            }

        }else{
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }

    }

}